import 'package:app/common/colors.dart';
import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/utils/search_module.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import '../../common/app_theme.dart';
import '../../components/nodata_page.dart';
import '../../router/app_pages.dart';
import 'leave_controller.dart';
import 'leave_detail_module/leave_detail_view.dart';
import 'leave_model.dart';

class LeaveView extends GetView{


  LeaveController controller = Get.put(LeaveController());


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text('请假'),
        action: [
          TextButton(onPressed: ()async{
           var data= await Get.toNamed(AppRoutes.createLeave);
           if(data =='refresh'){
             controller.requestData();
           }
          }, child: Text('新建',style: TextStyle(color: Colors.white),))
        ],
      ),
      body: GetBuilder<LeaveController>(builder: (_){
        return GestureDetector(
          onPanDown: (_) {
            /// 列表滑动响应
            SearchModule.closeKeyboard(context);
          },

          onTap: (){
            SearchModule.closeKeyboard(context);
          },
          child: Column(
            children: [
              Container(
                  decoration: BoxDecoration(
                      color: Colors.grey[200],
                      borderRadius: BorderRadius.all(Radius.circular(13))
                  ),
                  width: Get.width-50,
                  alignment: Alignment.centerLeft,
                  margin: EdgeInsets.only(top: 10,left: 10,right: 10),
                  padding: EdgeInsets.only(left: 15),
                  height: 35,
                  child: Row(
                    children: [
                      Icon(Icons.search,size: 20,color: AppColor.smallTextColor,),
                      Container(
                        alignment: Alignment.center,
                        color: Colors.transparent,
                        width: Get.width-150,
                        height: 35,
                        child: TextField(
                          controller: controller.editingController,
                          onChanged: (values){
                            print(values);
                            controller.requestData(search: values);
                          },
                          decoration: InputDecoration(
                              isCollapsed: true,
                              border: InputBorder.none,
                              hintText: '请输入关键词',hintStyle: TextStyle(
                              color: AppColor.hintColor,fontSize: 13
                          )
                          ),
                          style: TextStyle(color: AppColor.smallTextColor, fontSize: 14),
                        ),
                      )
                    ],
                  )
              ),
              Expanded(child:  EasyRefresh.custom(
                  onRefresh: ()=>controller.onRefresh(),
                  onLoad: ()=> controller.onLoad(),
                  emptyWidget: controller.dataArr.isNotEmpty?null:NoDataPage(),
                  header: MaterialHeader(),
                  footer: MaterialFooter(),
                  slivers: [

                    SliverList(
                      delegate: _mySliverChildBuilderDelegate(),
                    ),
                  ])),
            ],
          ),
        );
      }),
    );
  }
  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {

            LeaveItemModel model = controller.dataArr[index];
        return GestureDetector(
          onTap: () {
            Get.toNamed(AppRoutes.leaveDetail,arguments: model.id);
          },
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [

              Container(
                width: Get.width-10,
                color: Colors.white,
                padding: EdgeInsets.all(10),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [

                    Container(
                      padding: EdgeInsets.only(left: 5,right: 5),
                      child: Text('${model.userName!}提交的${model.name}',
                        style: appThemeData.textTheme.bodyText1!.
                        copyWith(fontWeight: FontWeight.w500,fontSize: 18),),
                    ),

                    Container(
                      padding: EdgeInsets.only(left: 5,right: 5,top: 5),
                      child: Column(
                        children: [
                          Text('开始时间:${model.dateStart}',style: appThemeData.textTheme.subtitle1,),
                          Text('结束时间:${model.dateStart}',style: appThemeData.textTheme.subtitle1,),
                        ],
                      ),
                    ),

                    Container(
                      padding: EdgeInsets.only(left: 5,right: 5),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text('状态：${model.state=='draft'?'待提交':model.state=='confirm'?'待批准':'已拒绝'}'),
                        ],
                      ),
                    ),

                    // Text('请假类型：${model.typeName!}'),
                    // Text('开始时间：${model.dateStart!}'),
                    // Text('结束时间：${model.dateEnd!}'),
                    // Text('持续时间：${model.duration!}'),
                    SizedBox(height: 15,),
                    Container(
                      padding: EdgeInsets.only(left: 5,right: 5),
                      child: Text('由${model.userName}提交',style: appThemeData.textTheme.bodyText1!.
                      copyWith(fontWeight: FontWeight.w500,fontSize: 14),),
                    )
                  ],
                ),
              ),
              Container(
                height: 0.5,
                color: AppColor.lineColor,
              )
            ],
          )
        );
      },
      childCount: controller.dataArr.length,
    );
  }

}